<%@ Page language="vb" CodeFile="addVehicleStep6.aspx.vb" Inherits="addVehicleStep6_aspx_vb" %>

<%
    Dim strSelected As String
    Dim selArray As Array
    rowNumber = 0
    partnerID = Request.QueryString("partnerID")
    dealerID = Request.QueryString("dealerID")
    intClientID = Request.QueryString("clientID")
    driverID = Request.QueryString("driverID")
    brandID = Request.QueryString("brandID")
    modelID = Request.QueryString("modelID")
    regnr = Request.QueryString("regnr")
    vinNumber = Request.QueryString("vinNumber")
    
    contractNumber = Request.QueryString("contractNumber")
    cylinderVolume = Request.QueryString("cylinderVolume")
    productID = Request.QueryString("productID")
    priceListDuration = Request.QueryString("priceListDuration")
    priceListDistance = Request.QueryString("priceListDistance")
    leaseStartDate = Request.QueryString("leaseStartDate")
    If Len(partnerID) = 0 Or Len(dealerID) = 0 Or Len(intClientID) = 0 Or Len(driverID) = 0 Or Len(brandID) = 0 Or Len(modelID) = 0 Then
        Response.Redirect("addVehicleStep1.aspx?subDo=%20-%20CHOOSE%20A%20PARTNER")
    Else
        partnerID = CLng(partnerID)
        dealerID = CLng(dealerID)
        intClientID = CLng(intClientID)
        driverID = CLng(driverID)
        brandID = CLng(brandID)
        modelID = CLng(modelID)
    End If
    If Len(productID) > 0 Then 
        productID = CLng(productID)
    End IF
    If Len(priceListDuration) > 0 Then 
        priceListDuration = CLng(priceListDuration)
    End IF
    If Len(priceListDistance) > 0 Then 
        priceListDistance = CLng(priceListDistance)
    End IF
    dbOpen()
    RecSet = Conn.Execute("Select cl.countryID, c.countryCurrency From (tblClient cl INNER JOIN tblCountry c ON c.countryID = cl.countryID) Where cl.clientID = " & intClientID & "")
    If RecSet.EOF Then 
        Response.Redirect("addVehicleStep1.aspx?subDo=%20-%20CHOOSE%20A%20PARTNER")
    Else
        countryID = RecSet.Fields("countryID").Value
        countryCurrency = RecSet.Fields("countryCurrency").Value
    End IF
    RecSet.Close()
    RecSet = Nothing
    dbClose()
    If Len(productID) > 0 Then 
        dbOpen()
        RecSet = Conn.Execute("Select Count(additionalProductID) additionalProductCount From tblAdditionalProduct Where productID = " & productID & " And byDefault = 0")
        additionalProductCount = RecSet.Fields("additionalProductCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
    End IF
    realtime = Now()
    year = Left(realtime, 4)
    month = Mid(realtime, 6, 2)
    day = Mid(realtime, 9, 2)
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        Over()
        strJS = ""
        strJS = strJS + "[""productID"", ""<1""],"
        strJS = strJS + "[""priceListDuration"", ""<1""],"
        strJS = strJS + "[""priceListDistance"", ""<1""],"
        strJS = strJS + "[""priceListID"", ""<1""],"
        strJS = strJS + "[""startDateYear"", ""<1""],"
        strJS = strJS + "[""startDateMonth"", ""<1""],"
        strJS = strJS + "[""startDateDay"", ""<1""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CHOOSE A PRODUCT - STEP 6 OF 7 <font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='addVehicleStep6.aspx?do=changeProductID&partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "' method='post'>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Product:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='productID' id='productID' onchange=""submit();"">")
        Response.Write("<option value=''> Select product  ")
        dbOpen()
        RecSet = Conn.Execute("Select productID, product From tblProduct Where countryID = " & countryID & " Order By product")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                dbProductID = RecSet.Fields("productID").Value
                dbProduct = RecSet.Fields("product").Value
                Response.Write("<option value='" & dbProductID & "'")
                If CStr(productID) = dbProductID Then 
                    Response.Write(" selected='selected'")
                End IF
                Response.Write("> " & dbProduct & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> ------------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_productID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<form action='addVehicleStep6.aspx?do=changePriceListDuration&partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "' method='post'>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Duration:</td>")
        Response.Write("<td align='right'>")
        If Len(productID) > 0 Then 
            Response.Write("<select name='priceListDuration' id='priceListDuration' onchange=""submit();"">")
            Response.Write("<option value=''> Select duration  ")
            dbOpen()
            RecSet = Conn.Execute("Select priceListDuration From tblPriceList Where productID = " & productID & " GROUP BY priceListDuration ORDER BY priceListDuration")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    dbPriceListDuration = RecSet.Fields("priceListDuration").Value
                    Response.Write("<option value='" & dbPriceListDuration & "'")
                    If CStr(priceListDuration) = dbPriceListDuration Then 
                        Response.Write(" selected='selected'")
                    End IF
                    Response.Write("> " & dbPriceListDuration & " months")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        Else
            Response.Write("<select name='priceListDuration' id='priceListDuration' disabled>")
            Response.Write("<option value=''> - Select duration - ")
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        End IF
        Response.Write("</td>")
        Response.Write("<td><div id='error_priceListDuration' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<form action='addVehicleStep6.aspx?do=changePriceListDistance&partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&priceListDuration=" & priceListDuration & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "' method='post'>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Driving distance:</td>")
        Response.Write("<td align='right'>")
        If Len(priceListDuration) > 0 Then 
            Response.Write("<select name='priceListDistance' id='priceListDistance' onchange=""submit();"">")
            Response.Write("<option value=''> Select distance  ")
            dbOpen()
            RecSet = Conn.Execute("Select priceListDistance From tblPriceList Where productID = " & productID & " And priceListDuration = " & priceListDuration & " GROUP BY priceListDistance ORDER BY priceListDistance")
            If Not RecSet.EOF Then 
                Do Until RecSet.EOF
                    dbPriceListDistance = RecSet.Fields("priceListDistance").Value
                    Response.Write("<option value='" & dbPriceListDistance & "'")
                    If CStr(priceListDistance) = dbPriceListDistance Then 
                        Response.Write(" selected='selected'")
                    End IF
                    Response.Write("> " & dbPriceListDistance & " km")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        Else
            Response.Write("<select name='priceListDistance' id='priceListDistance' disabled>")
            Response.Write("<option value=''> - Select distance - ")
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        End IF
        Response.Write("</td>")
        Response.Write("<td><div id='error_priceListDistance' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<form action='addVehicleStep6.aspx?do=changePriceListID&partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&priceListDuration=" & priceListDuration & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Price category:</td>")
        Response.Write("<td align='right'>")
        If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
            Response.Write("<select name='priceListID' id='priceListID'>")
            Response.Write("<option value=''> Select category  ")
            dbOpen()
            RecSet = Conn.Execute("Select priceListID, priceListCategory, price From tblPriceList Where productID = " & productID & " And priceListDuration = " & priceListDuration & " And priceListDistance = " & priceListDistance & " And cylinderVolumeFrom <= " & cylinderVolume & " And cylinderVolumeTo >= " & cylinderVolume & " ORDER BY priceListCategory")
            If RecSet.EOF Then 
                Response.Write("<option value=''> No prices found ")
            Else
                Do Until RecSet.EOF
                    dbPriceListID = RecSet.Fields("priceListID").Value
                    dbPriceListCategory = RecSet.Fields("priceListCategory").Value
                    dbPrice = RecSet.Fields("price").Value
                    Response.Write("<option value='" & dbPriceListID & "'> " & dbPriceListCategory & " - " & dbPrice & " " & countryCurrency & "")
                    RecSet.MoveNext()
                Loop
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        Else
            Response.Write("<select name='priceListID' id='priceListID' disabled>")
            Response.Write("<option value=''> - Select category - ")
            Response.Write("<option value=''> ------------------------------------------- ")
            Response.Write("</select>")
        End IF
        Response.Write("</td>")
        Response.Write("<td><div id='error_priceListID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        If CDbl(additionalProductCount) > 0 Then 
            Response.Write("<tr>")
            Response.Write("<td class='formBold' valign='top'>Additional Product(s):&nbsp; </td>")
            Response.Write("<td align='right'>")
            If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
                Response.Write("<select name='additionalProductID' id='additionalProductID' multiple='multiple' style='width: 242px; height: 50px'>")
                dbOpen()
                RecSet = Conn.Execute("Select additionalProductID, additionalProduct From tblAdditionalProduct Where productID = " & productID & " And byDefault = 0 Order by productID")
                If Not RecSet.EOF Then 
                    Do Until RecSet.EOF
                        additionalProductID = RecSet.Fields("additionalProductID").Value
                        additionalProduct = RecSet.Fields("additionalProduct").Value
                        Response.Write("<option value='" & additionalProductID & "'> " & additionalProduct & " </option>")
                        RecSet.MoveNext()
                    Loop
                End IF
                RecSet.Close()
                RecSet = Nothing
                dbClose()
                Response.Write("</select>")
            Else
                Response.Write("<select name='additionalProductID' id='additionalProductID' multiple='multiple' style='width: 242px; height: 50px' disabled='disabled'>")
                Response.Write("</select>")
            End IF
            Response.Write("</td>")
            Response.Write("</tr>")
        End IF
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Lease contract start date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
            Response.Write("<select name='startDateYear' id='startDateYear'>")
            Response.Write("<option value=''> - Year - ")
            stopYear = CDbl(year) + 3
            year = year - CStr(2)
            yearCounter = year
            For yearLoop = year To stopYear
                Response.Write("<option value='" & yearCounter & "'")
                'If yearCounter = year Then
                If CInt(yearCounter) = 2012 Then 
                    Response.Write("selected='selected'")
                End IF
                Response.Write("> " & yearCounter & " ")
                yearCounter = yearCounter + 1
            Next
            Response.Write("</select>")
        Else
            Response.Write("<select name='startDateYear' id='startDateYear' disabled>")
            Response.Write("<option value=''> - Year - ")
            Response.Write("</select>")
        End IF
        Response.Write(" - ")
        If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
            Response.Write("<select name='startDateMonth' id='startDateMonth'>")
            Response.Write("<option value=''> - Month - ")
            yearCounter = 1
            For yearLoop = 1 To 12
                Response.Write("<option value='" & yearCounter & "'")
                If CLng(yearCounter) = CLng(month) Then 
                    Response.Write("selected='selected'")
                End IF
                Response.Write("> " & yearCounter & " ")
                yearCounter = yearCounter + 1
            Next
            Response.Write("</select>")
        Else
            Response.Write("<select name='startDateMonth' id='startDateMonth' disabled>")
            Response.Write("<option value=''> - Month - ")
            Response.Write("</select>")
        End IF
        Response.Write(" - ")
        If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
            Response.Write("<select name='startDateDay' id='startDateDay'>")
            Response.Write("<option value=''> - Day - ")
            yearCounter = 1
            For yearLoop = 1 To 31
                Response.Write("<option value='" & yearCounter & "'")
                If CLng(yearCounter) = CLng(day) Then 
                    Response.Write("selected='selected'")
                End IF
                Response.Write("> " & yearCounter & " ")
                yearCounter = yearCounter + 1
            Next
            Response.Write("</select>")
        Else
            Response.Write("<select name='startDateDay' id='startDateDay' disabled>")
            Response.Write("<option value=''> - Day - ")
            Response.Write("</select>")
        End IF
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_startDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_startDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_startDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        If Len(priceListDistance) > 0 And Len(cylinderVolume) > 0 Then 
            Response.Write("<tr><td colspan='2'><textarea name='vehicleDocumentation' style='width: 100%; height: 50px'></textarea></td></tr>")
        Else
            Response.Write("<tr><td colspan='2'><textarea name='vehicleDocumentation' style='width: 100%; height: 50px' disabled='disabled'></textarea></td></tr>")
        End IF
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Step 7'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>SUBSCRIPTION PROCESS<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select d.dealer, p.partnerName, c.country From ((tblDealer d INNER JOIN tblPartner p ON p.partnerID = d.partnerID) INNER JOIN tblCountry c ON c.countryID = p.countryID) Where d.dealerID = " & dealerID & "")
        If RecSet.EOF Then 
            Response.Redirect("addVehicleStep1.aspx?subDo=%20-%20CHOOSE%20A%20PARTNER")
        Else
            dealer = RecSet.Fields("dealer").Value
            partnerName = RecSet.Fields("partnerName").Value
            country = RecSet.Fields("country").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select d.driver, c.client From (tblDriver d INNER JOIN tblClient c ON c.clientID = d.clientID) Where d.driverID = " & driverID & "")
        If RecSet.EOF Then 
            Response.Redirect("addVehicleStep1.aspx?subDo=%20-%20CHOOSE%20A%20PARTNER")
        Else
            driver = RecSet.Fields("driver").Value
            client = RecSet.Fields("client").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select b.brand, m.model From (tblModel m INNER JOIN tblBrand b ON b.brandID = m.brandID) Where m.modelID = " & modelID & "")
        If RecSet.EOF Then 
            Response.Redirect("addVehicleStep1.aspx?subDo=%20-%20CHOOSE%20A%20PARTNER")
        Else
            brand = RecSet.Fields("brand").Value
            model = RecSet.Fields("model").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr bgcolor='#eeeeee'><td>Country:</td><td>" & country & "</td></tr>")
        Response.Write("<tr><td>Partner:</td><td>" & partnerName & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Car dealer:</td><td>" & dealer & "</td></tr>")
        Response.Write("<tr><td>Client:</td><td>" & client & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Driver:</td><td>" & driver & "</td></tr>")
        Response.Write("<tr><td>Vehicle:</td><td>" & brand & " " & model & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Licence number:</td><td>" & regnr & "</td></tr>")
        Response.Write("<tr><td>VIN-number:</td><td>" & vinNumber & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Contract number:</td><td>" & contractNumber & "</td></tr>")
        Response.Write("<tr><td>Cylinder volume:</td><td>" & cylinderVolume & " cc</td></tr>")
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        If Len(leaseStartDate) > 0 Then 
            Response.Write("<tr><td colspan='2'><br></td></tr>")
            Response.Write("<tr><td class='headline' colspan='2'>INFORMATION<hr class='hrHeadline'></td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td colspan='2'><font color='red'>OBS!</font> " & leaseStartDate & " is not a valid date. Please change the lease contract start date and try again.</td></tr>")
            Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        End IF
        If vinNumber = "N/A" Then 
            vinNumber = "-"
        ElseIf regnr = "N/A" Then 
            regnr = "-"
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As vehicleCount From tblVehicle Where countryID = " & countryID & " And (regnr = '" & regnr & "' Or vinNumber = '" & vinNumber & "')")
        If Not RecSet.EOF Then 
            vehicleCount = RecSet.Fields("vehicleCount").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CDbl(vehicleCount) > 0 Then 
            Response.Write("<tr><td colspan='2'><br></td></tr>")
            Response.Write("<tr><td class='headline' colspan='2'>INFORMATION<hr class='hrHeadline'></td></tr>")
            Response.Write("<tr bgcolor='#eeeeee'><td colspan='2'><font color='red'>OBS!</font> The vehicles licence number or VIN-number was found on a already existing lease contract in the system.</td></tr>")
            Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select pvd.preVehicleDocumentation, pvd.date, u.username From (tblPreVehicleDocumentation pvd INNER JOIN tblUser u ON u.userID = pvd.userID) Where pvd.countryID = " & countryID & " And (pvd.regnr = '" & regnr & "' Or pvd.vinNumber = '" & vinNumber & "') Order By pvd.preVehicleDocumentationID desc")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                preVehicleDocumentation = RecSet.Fields("preVehicleDocumentation").Value
                __date = RecSet.Fields("date").Value
                username = RecSet.Fields("username").Value
                __date = Left(__date, 10)
                Response.Write("<tr><td colspan='2'><br></td></tr>")
                Response.Write("<tr><td class='headline' colspan='2'>DOCUMENTATION<hr class='hrHeadline'></td></tr>")
                Response.Write("<tr bgcolor='#eeeeee'><td colspan='2'><font color='red'>Future documentation by " & username & " on " & __date & ".</font><br>" & preVehicleDocumentation & "</td></tr>")
                Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "changeProductID" Then 
        productID = Request.Form("productID")
        Response.Redirect("addVehicleStep6.aspx?partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "")
    ElseIf Request.QueryString("do") = "changePriceListDuration" Then 
        priceListDuration = Request.Form("priceListDuration")
        Response.Redirect("addVehicleStep6.aspx?partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&priceListDuration=" & priceListDuration & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "")
    ElseIf Request.QueryString("do") = "changePriceListDistance" Then 
        priceListDistance = Request.Form("priceListDistance")
        Response.Redirect("addVehicleStep6.aspx?partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&priceListDuration=" & priceListDuration & "&priceListDistance=" & priceListDistance & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "")
    ElseIf Request.QueryString("do") = "changePriceListID" Then 
        priceListID = Request.Form("priceListID")
        startDateYear = Request.Form("startDateYear")
        startDateMonth = Request.Form("startDateMonth")
        startDateDay = Request.Form("startDateDay")
        additionalProductID = Request.Form("additionalproductID")
        vehicleDocumentation = Request.Form("vehicleDocumentation")
        startDateYear = CLng(startDateYear)
        startDateMonth = CLng(startDateMonth)
        startDateDay = CLng(startDateDay)
        If CLng(startDateMonth) < 10 Then 
            startDateMonth = "0" & startDateMonth & ""
        End IF
        If CLng(startDateDay) < 10 Then 
            startDateDay = "0" & startDateDay & ""
        End IF
        leaseStartDate = startDateYear & "-" & startDateMonth & "-" & startDateDay
        If IsDate(leaseStartDate) = false Then 
            Response.Redirect("addVehicleStep6.aspx?partnerID=" & partnerID & "&dealerID=" & dealerID & "&clientID=" & intClientID & "&driverID=" & driverID & "&productID=" & productID & "&leaseStartDate=" & leaseStartDate & "&priceListDuration=" & priceListDuration & "&priceListDistance=" & priceListDistance & "&brandID=" & brandID & "&modelID=" & modelID & "&regnr=" & regnr & "&vinNumber=" & vinNumber & "&contractNumber=" & contractNumber & "&cylinderVolume=" & cylinderVolume & "")
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select pl.productID, pl.adminFee, p.allowInvoiceAdminFee From (tblPriceList pl INNER JOIN tblProduct p ON p.productID = pl.productID) Where priceListID = " & priceListID & "")
        productID = RecSet.Fields("productID").Value
        adminFee = RecSet.Fields("adminFee").Value
        allowInvoiceAdminFee = RecSet.Fields("allowInvoiceAdminFee").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        userID = fragusLeasingUserID
        active = 1
        dbOpen()
        Response.Write("Insert Into tblVehicle (countryID, brandID, modelID, productID, priceListID, userID, partnerID, dealerID, clientID, driverID, regnr, vinNumber, cylinderVolume, contractNumber, active, leaseStartDate) Values(" & countryID & "," & brandID & "," & modelID & "," & productID & "," & priceListID & "," & userID & "," & partnerID & "," & dealerID & "," & intClientID & "," & driverID & ",'" & regnr & "','" & vinNumber & "'," & cylinderVolume & ",'" & contractNumber & "'," & active & ",'" & leaseStartDate & "'<br>")
        Conn.Execute("Insert Into tblVehicle (countryID, brandID, modelID, productID, priceListID, userID, partnerID, dealerID, clientID, driverID, regnr, vinNumber, cylinderVolume, contractNumber, active, leaseStartDate) Values(" & countryID & "," & brandID & "," & modelID & "," & productID & "," & priceListID & "," & userID & "," & partnerID & "," & dealerID & "," & intClientID & "," & driverID & ",'" & regnr & "','" & vinNumber & "'," & cylinderVolume & ",'" & contractNumber & "'," & active & ",'" & leaseStartDate & "')")
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("SELECT MAX(vehicleID) AS vehicleID FROM tblVehicle")
        vehicleID = RecSet.Fields("vehicleID").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Len(additionalProductID) > 0 Then 
            dbOpen()
            strSelected = Request.Form("additionalproductID")
            selArray = Split(strSelected, ",")
            For i = 0 To UBound(selArray)
                additionalProductID = Convert.ToUInt32(selArray(i))
                Conn.Execute("INSERT INTO tblAdditionalProductVehicleContent(additionalProductID,vehicleID) values(" & additionalProductID & "," & vehicleID & ")")
            Next
            dbClose()
        End If
        If vinNumber = "N/A" Then 
            vinNumber = "-"
        ElseIf regnr = "N/A" Then 
            regnr = "-"
        End IF
        If Len(vehicleDocumentation) > 0 Then 
            vehicleDocumentation = Trim(vehicleDocumentation)
            vehicleDocumentation = Replace(vehicleDocumentation, """", "&quot;")
            vehicleDocumentation = Replace(vehicleDocumentation, "'", "&#39;")
            vehicleDocumentation = Replace(vehicleDocumentation, vbCrLf, "<br>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[b]", "<b>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[/b]", "</b>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[i]", "<i>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[/i]", "</i>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[u]", "<u>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[/u]", "</u>")
            vehicleDocumentation = Replace(vehicleDocumentation, "[c]", "<font color=""red"">")
            vehicleDocumentation = Replace(vehicleDocumentation, "[/c]", "</font>")
            dbOpen()
            Conn.Execute("Insert Into tblVehicleDocumentation (vehicleID, userID, vehicleDocumentation) Values(" & vehicleID & "," & userID & ",'" & vehicleDocumentation & "')")
            dbClose()
        End IF
        dbOpen()
        RecSet = Conn.Execute("Select preVehicleDocumentationID, preVehicleDocumentation, userID, date From tblPreVehicleDocumentation Where countryID = " & countryID & " And (regnr = '" & regnr & "' Or vinNumber = '" & vinNumber & "')")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                preVehicleDocumentationID = RecSet.Fields("preVehicleDocumentationID").Value
                preVehicleDocumentation = RecSet.Fields("preVehicleDocumentation").Value
                userID = RecSet.Fields("userID").Value
                __date = RecSet.Fields("date").Value
                Response.Write("Insert Into tblVehicleDocumentation (vehicleID, userID, vehicleDocumentation, date) Values(" & vehicleID & "," & userID & ",'" & preVehicleDocumentation & "','" & __date & "')<br>")
                Conn.Execute("Insert Into tblVehicleDocumentation (vehicleID, userID, vehicleDocumentation, date) Values(" & vehicleID & "," & userID & ",'" & preVehicleDocumentation & "','" & __date & "')")
                Response.Write("Delete From tblVehicleDocumentation Where preVehicleDocumentationID = " & preVehicleDocumentationID & "<br>")
                Conn.Execute("Delete From tblPreVehicleDocumentation Where preVehicleDocumentationID = " & preVehicleDocumentationID & "")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select invoiceID From tblInvoice Where partnerID = " & partnerID & " And userID IS NULL")
        If RecSet.EOF Then 
            Conn.Execute("Insert Into tblInvoice (countryID, partnerID) Values(" & countryID & "," & partnerID & ")")
            invoiceID = 0
        Else
            invoiceID = RecSet.Fields("invoiceID").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(invoiceID) = 0 Then 
            dbOpen()
            RecSet = Conn.Execute("SELECT MAX(invoiceID) AS invoiceID FROM tblInvoice")
            invoiceID = RecSet.Fields("invoiceID").Value
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        If CDbl(adminFee) > 0 And CDbl(allowInvoiceAdminFee) = 1 Then 
            dbOpen()
            RecSet = Conn.Execute("Select invoiceAdminFeeID From tblInvoiceAdminFee Where partnerID = " & partnerID & " And userID IS NULL")
            If RecSet.EOF Then 
                Conn.Execute("Insert Into tblInvoiceAdminFee (countryID, partnerID, invoiceID) Values(" & countryID & "," & partnerID & "," & invoiceID & ")")
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Response.Write("leaseStartDate = " & leaseStartDate & "<br>")
        checkFirstDay = Right(leaseStartDate, 2)
        Response.Write("checkFirstDay = " & checkFirstDay & "<br>")
        If checkFirstDay = "01" Then 
            leaseFirstPeriodDate = leaseStartDate
            Response.Write("leaseFirstPeriodDate = " & leaseFirstPeriodDate & "<br>")
        Else
            leaseFirstPeriodDate = Left(leaseStartDate, 8)
            leaseFirstPeriodDate = leaseFirstPeriodDate & "01"
            leaseFirstPeriodDate = DateAdd("m", 1, Convert.ToDateTime(leaseFirstPeriodDate))
            Response.Write("leaseFirstPeriodDate = " & leaseFirstPeriodDate & "<br>")
        End IF
        dbOpen()
       ' RecSet = Conn.Execute("Select Top 1 ic.leaseCurrentDate From (tblInvoiceContent ic INNER JOIN tblInvoice i ON i.invoiceID = ic.InvoiceID) Where ic.invoiceID = " & invoiceID & " And i.partnerID = " & partnerID & " order by ic.leaseCurrentDate desc")
        RecSet = Conn.Execute(" Select Top 1 leaseCurrentDate From tblInvoiceContent where date='1900 00:00:00'")
        If RecSet.EOF Then
            realtime = Left(realtime, 8) & "01"
        Else
            realtime = RecSet.Fields("leaseCurrentDate").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("realTime = " & realtime & "<br>")
        
      
        leaseStartDateDiff = DateDiff("m", Convert.ToDateTime(leaseFirstPeriodDate), Convert.ToDateTime(realtime))
        Response.Write("leaseStartDateDiff = " & leaseStartDateDiff & "<br>")
        leaseCurrentDate = leaseFirstPeriodDate
        dbOpen()
        For leaseStartDateDiffLoop = 0 To leaseStartDateDiff
            Response.Write("leaseCurrentDate = " & leaseCurrentDate & "<br>")
            Response.Write("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & ")<br>")
            Conn.Execute("Insert Into tblInvoiceContent (invoiceID, vehicleID, priceListID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & priceListID & ",'" & leaseCurrentDate & "')")
           
            
            If Len(additionalProductID) > 0 Then
                strSelected = Request.Form("additionalproductID")
                selArray = Split(strSelected, ",")
                For i = 0 To UBound(selArray)
                    additionalProductID = Convert.ToUInt32(selArray(i))
                    RecSet = Conn.Execute("Select price From tblAdditionalProduct Where additionalProductID = " & additionalProductID & "")
                    If Not RecSet.EOF Then
                        additionalPrice = RecSet.Fields("price").Value
                    End If
                    RecSet.Close()
                    RecSet = Nothing
                    additionalPrice = 1
                    Response.Write("additionalPrice = " & additionalPrice & "<br>")
                    If CInt(additionalPrice) > 0 Then
                        Response.Write("INSERT INTO tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDate & "')<br>")
                        Conn.Execute("INSERT INTO tblInvoiceContentAdditional (invoiceID, vehicleID, additionalProductID, leaseCurrentDate) Values(" & invoiceID & "," & vehicleID & "," & additionalProductID & ",'" & leaseCurrentDate & "')")
                    End If
                Next
            End If
            leaseCurrentDate = DateAdd("m", 1, Convert.ToDateTime(leaseCurrentDate))
        Next
        dbClose()
        Response.Redirect("addVehicleStep7.aspx?vehicleID=" & vehicleID & "")
    End IF
%>
